from django.db import models
from django.conf import settings

class VoiceProcessingJob(models.Model):
    JOB_STATUS_CHOICES = (
        ('uploaded', '已上传'),
        ('transcribing', '转写中'),
        ('transcribed', '已转写'),
        ('parsing', '解析中'),
        ('parsed', '已解析'),
        ('validated', '已验证'),
        ('failed', '处理失败'),
        ('executing', '执行中(K8S)'),
        ('completed', '已完成'),
    )
    
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="用户")
    audio_file = models.FileField(upload_to='audio_uploads/%Y/%m/%d/', verbose_name="语音文件")
    original_text = models.TextField(blank=True, null=True, verbose_name="识别文本")
    parsed_command = models.JSONField(blank=True, null=True, verbose_name="解析后的指令")
    target_namespace = models.CharField(max_length=100, blank=True, null=True, verbose_name="目标命名空间")
    is_valid = models.BooleanField(default=False, verbose_name="指令是否有效")
    validation_message = models.TextField(blank=True, null=True, verbose_name="验证信息")
    status = models.CharField(max_length=20, choices=JOB_STATUS_CHOICES, default='uploaded', verbose_name="处理状态")
    k8s_response = models.JSONField(blank=True, null=True, verbose_name="K8S API响应")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    
    class Meta:
        verbose_name = '语音处理任务'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']
    
    def __str__(self):
        return f"语音任务 {self.id} - {self.user.username} ({self.status})"